# -*- coding: utf-8 -*-
# created on 2016/12/22

from sympy import sympify
from sympy.core.function import UndefinedFunction
from mathsolver.functions.base import BaseFunction, BaseValue, new_latex
from mathsolver.functions.hanshu.helper import huajian
from mathsolver.functions.sympy_utils import get_all_child


class JieXiShiPanDuanLeiBie(BaseFunction):
    """由函数解析式满足条件判断是否属于这类函数"""

    def solver(self, *args):
        # 处理输入
        funceq, func = args
        left_eq, right_eq = sympify(funceq.value)
        left_eq_o, right_eq_o = left_eq, right_eq
        func_var, func_expr = func.var, func.expression

        # 抽取出所有的 f(*) 表达式
        funcs_left = get_all_child(left_eq, lambda x: isinstance(type(x), UndefinedFunction))
        for fx in funcs_left:
            f_arg = fx.args[0]
            left_eq = left_eq.subs(fx, func_expr.subs(func_var, f_arg))

        funcs_right = get_all_child(right_eq, lambda x: isinstance(type(x), UndefinedFunction))
        for fx in funcs_right:
            f_arg = fx.args[0]
            right_eq = right_eq.subs(fx, func_expr.subs(func_var, f_arg))

        self.steps.append(["", "代入得 %s = %s, %s = %s" %
                           (new_latex(left_eq_o), new_latex(left_eq), new_latex(right_eq_o), new_latex(right_eq))])

        # 判断解析式是否相同
        if huajian(left_eq - right_eq) == 0:
            res = True
            self.steps.append(["", "函数解析式相同，所以 %s 满足条件" % func.printing()])
        else:
            res = False
            self.steps.append(["", "函数解析式不同，所以 %s 不满足条件" % func.printing()])

        self.label.add("由函数解析式满足条件判断是否属于这类函数")
        self.output.append(BaseValue(res))
        return self


if __name__ == '__main__':
    pass
